﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace WindowsFormsApplication1.Filtry
{
    class MedianaSzum
    {
        public Bitmap bitmapFiltr { get; set; }
        private int MacierzWidthIHight,width,height;
        private Bitmapp bitmap ;

        public MedianaSzum(int MacierzWidthIHight, int width, int height, Bitmapp bitmap)
        {
            this.width = width; this.height = height; 
            this.bitmap = bitmap; this.MacierzWidthIHight = MacierzWidthIHight;
        }

        public Bitmap ZwrocObrazekWyszumiony()
        {
            this.bitmapFiltr = new Bitmap(this.width, this.height);
            Double wynikR = 0, wynikG = 0, wynikB = 0;
            Double[] tablicaR = new Double[this.MacierzWidthIHight * this.MacierzWidthIHight],
                tablicaG = new Double[this.MacierzWidthIHight * this.MacierzWidthIHight],
                tablicaB = new Double[this.MacierzWidthIHight * this.MacierzWidthIHight];

            for (int i = 0; i < this.height; i++)
            {
                for (int j = 0; j < this.width; j++)
                {
                    if (j <= MacierzWidthIHight / 2 || j > this.width - MacierzWidthIHight / 2 - 1 || i <= MacierzWidthIHight / 2 || i > this.height - MacierzWidthIHight / 2 - 1)
                    {
                        bitmapFiltr.SetPixel(j, i, bitmap.GetPixel(j, i));
                    }
                    else
                    {
                        int xPixel = j - MacierzWidthIHight / 2, yPixel = i - MacierzWidthIHight / 2;
                        int index = 0;
                        for (int ii = 0; ii < MacierzWidthIHight; ii++)
                        {
                            for (int jj = 0; jj < MacierzWidthIHight; jj++)
                            {
                                tablicaR[index] =  Convert.ToDouble(bitmap.GetPixelR(xPixel, yPixel));
                                tablicaG[index] = Convert.ToDouble(bitmap.GetPixelG(xPixel, yPixel));
                                tablicaB[index] = Convert.ToDouble(bitmap.GetPixelB(xPixel, yPixel));
                                xPixel++; index++;
                            }
                            yPixel++; xPixel = j - MacierzWidthIHight / 2;
                        }
                        Array.Sort(tablicaR);
                        Array.Sort(tablicaG);
                        Array.Sort(tablicaB);
                        wynikR = tablicaR[((this.MacierzWidthIHight * this.MacierzWidthIHight + 1) / 2)];
                        wynikB = tablicaB[((this.MacierzWidthIHight * this.MacierzWidthIHight + 1) / 2)];
                        wynikG = tablicaG[((this.MacierzWidthIHight * this.MacierzWidthIHight + 1) / 2)];
                        bitmapFiltr.SetPixel(j, i, Color.FromArgb((int)wynikR, (int)wynikG, (int)wynikB));
                        wynikR = 0; wynikG = 0; wynikB = 0;
                    }
                }
            }
            // a teraz krawedzie
            int i1 = MacierzWidthIHight / 2 + 1, i2 = this.height - MacierzWidthIHight / 2 - 1;
            int j1 = MacierzWidthIHight / 2 + 1, j2 = this.width - MacierzWidthIHight / 2 - 1;
            for (int i = 0; i < this.height; i++)
            {
                for (int j = 0; j < this.width; j++)
                {

                    if (i <= MacierzWidthIHight / 2)
                    {
                        bitmapFiltr.SetPixel(j, i, bitmapFiltr.GetPixel(j, i1));
                    }
                    if (i > this.height - MacierzWidthIHight / 2 - 1)
                    {
                        bitmapFiltr.SetPixel(j, i, bitmapFiltr.GetPixel(j, i2));
                    }
                    if (j <= MacierzWidthIHight / 2)
                    {
                        bitmapFiltr.SetPixel(j, i, bitmapFiltr.GetPixel(j1, i));
                    }
                    if (j > this.width - MacierzWidthIHight / 2 - 1)
                    {
                        bitmapFiltr.SetPixel(j, i, bitmapFiltr.GetPixel(j2, i));
                    }
                }
            }

            return this.bitmapFiltr;
        }
    }
}
